package cn.eric.game.fujiatianxia6.test;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Test;

import cn.eric.game.fujiatianxia6.po.City;
import cn.eric.game.fujiatianxia6.po.General;
import cn.eric.game.fujiatianxia6.po.Skill;

public class Dom4JforXML {
	
	private static List<General> listGeneral = new ArrayList<General>();
	
	private static List<Skill> skills = new ArrayList<>();

	@Test
	public void test() throws DocumentException {
		SAXReader reader = new SAXReader();
		
		Document document = reader.read(new File("data/base/Generals.xml"));
	    
		Element root = document.getRootElement();
		
		listNodes(root);
	}
	
	//
	private void listNodes(Element node) {
        System.out.println("当前节点的名称：" + node.getName());  
        //首先获取当前节点的所有属性节点  
        List<Attribute> list = node.attributes();  
        //遍历属性节点  
        for(Attribute attribute : list){  
            //System.out.println("属性"+attribute.getName() +":" + attribute.getValue());  
        }  
        //如果当前节点内容不为空，则输出  
        if(!(node.getTextTrim().equals(""))){  
             //System.out.println("节点"+ node.getName() + "：" + node.getText());    
        }  
        //同时迭代当前节点下面的所有子节点  
        //使用递归  
        Iterator<Element> iterator = node.elementIterator();  
        while(iterator.hasNext()){  
            Element e = iterator.next();  
            listNodes(e);  
        }  
		
	}
	
	@Test
	public static List<General> test2() throws DocumentException {
		SAXReader reader = new SAXReader();
		
		Document document = reader.read(new File("data/base/Generals.xml"));
	    
		Element root = document.getRootElement();
		
		listNodes2(root);
		return listGeneral;
	}
	
	@Test
	public static List<Skill> createSkills() throws DocumentException {
		SAXReader reader = new SAXReader();
		
		Document document = reader.read(new File("data/base/Skills.xml"));
	    
		Element root = document.getRootElement();
		
		listNodesSkill(root);
		return skills;
	}
	
	@SuppressWarnings("unchecked")
	private static void listNodesSkill(Element node) {
        //使用递归  
		Iterator<Element> iterator = node.elementIterator();  
        while(iterator.hasNext()){
        	Skill s = new Skill();
            Element e = iterator.next();   
            //遍历属性节点  
			Iterator iterator2 = e.elementIterator();
            while(iterator2.hasNext()){
            	Element e2 = (Element) iterator2.next();
                //如果当前节点内容不为空，则输出  
                if(!(e2.getTextTrim().equals(""))){  
                     switch(e2.getName()){
                     	case "name":
                     		s.setName(e2.getText());
                     		break;
                     	case "id":
                     		s.setId(Integer.parseInt(e2.getText()));
                     		break;
                     	case "memo":
                     		s.setMemo(e2.getText());
                     		break;
                     	case "data":
                     		s.setData(Integer.parseInt(e2.getText()));
                     		break;
                     	case "time":
                     		s.setTime(Integer.parseInt(e2.getText()));
                     		break;
                     }
                     
                }
            }
            skills.add(s);
        } 		
	}

	private static void listNodes2(Element node) {
        //System.out.println("当前节点的名称：" + node.getName());  
        //首先获取当前节点的所有属性节点  
        List<Attribute> list = node.attributes();  
        //遍历属性节点  
        for(Attribute attribute : list){  
            //System.out.println("属性"+attribute.getName() +":" + attribute.getValue());  
        }   
        //同时迭代当前节点下面的所有子节点  
        //使用递归  
        Iterator<Element> iterator = node.elementIterator();  
        while(iterator.hasNext()){
        	General g = new General();
            Element e = iterator.next();  
            //System.out.println("当前节点的名称：" + e.getName());
            //首先获取当前节点的所有属性节点  
            List<Attribute> listA = e.attributes();  
            //遍历属性节点  
            for(Attribute attribute : listA){  
                //System.out.println("属性"+attribute.getName() +":" + attribute.getValue()); 
                g.setId(attribute.getValue());
            }
            Iterator iterator2 = e.elementIterator();
            while(iterator2.hasNext()){
            	Element e2 = (Element) iterator2.next();
                //如果当前节点内容不为空，则输出  
                if(!(e2.getTextTrim().equals(""))){  
                     //System.out.println("节点"+ e2.getName() + "：" + e2.getText());
                     switch(e2.getName()){
                     	case "name":
                     		g.setName(e2.getText());
                     		break;
                     	case "king":
                     		g.setKing(e2.getText());
                     		break;
                     	case "attack":
                     		g.setAttack(e2.getText());
                     		break;
                     	case "command":
                     		g.setCommand(e2.getText());
                     		break;
                     	case "intelligence":
                     		g.setIntelligence(e2.getText());
                     		break;
                     	case "charm":
                     		g.setCharm(e2.getText());
                     		break;
                     	case "politics":
                     		g.setPolitics(e2.getText());
                     		break;
                     	case "vitality":
                     		g.setVitality(e2.getText());
                     		break;
                     	case "belongTo":
                     		g.setBelongTo(e2.getText());
                     		break;
                     	case "relation":
                     		g.setRelations(e2.getText());
                     		break;
                     	case "skill":
                     		g.setSkill(e2.getText());
                     		break;
                     	case "status":
                     		g.setStatus(e2.getText());
                     		break;
                     }
                     
                }
            }
            listGeneral.add(g);
        }  
		
	}
}
